package cn.com.dashihui.web.controller;

import java.util.ArrayList;
import java.util.List;

import com.jfinal.aop.Duang;
import com.jfinal.kit.PropKit;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Record;

import cn.com.dashihui.kit.ExcelExportUtil.Pair;
import cn.com.dashihui.web.base.BaseController;
import cn.com.dashihui.web.dao.Seller;
import cn.com.dashihui.web.service.SellerTongService;

public class SellerTongController extends BaseController{
	private SellerTongService sellerTongService = Duang.duang(SellerTongService.class);
	/**
	 * 首页
	 */
	public void index(){
		Record record=sellerTongService.countStore();
		setAttr("tolnumber", record.get("tolnumber"));
		setAttr("sjshnumber", record.get("sjshnumber"));
		setAttr("sjnumber", record.get("sjnumber"));
		setAttr("xjnumber", record.get("xjnumber"));
		render("index.jsp");
	}
	/**
	 * 分页查询
	 */
	 public void page(){
			int pageNum = getParaToInt(0, 1);
			int pageSize = getParaToInt("pageSize", PropKit.getInt("constants.pageSize"));
			String seller = getPara("seller");
//			String sellerId = getParaValues("sellerId");
			String sellerId = getPara("sellerId");
			String tel = getPara("tel");
			String beginDate = getPara("beginDate");
			String endDate = getPara("endDate");
			renderResult(0,sellerTongService.findByPage(pageNum, pageSize,seller,sellerId, tel,beginDate,endDate));
		}
	 /**
		 * 分页排序查询
		 */
		 public void page1(){
				int pageNum = getParaToInt(0, 1);
				int pageSize = getParaToInt("pageSize", PropKit.getInt("constants.pageSize"));
				String index=getPara("index");
				String seller = getPara("seller");
//				String sellerId = getParaValues("sellerId");
				String sellerId = getPara("sellerId");
				String tel = getPara("tel");
				String beginDate = getPara("beginDate");
				String endDate = getPara("endDate");
				renderResult(0,sellerTongService.findByPage1(pageNum, pageSize,index,seller,sellerId, tel,beginDate,endDate));
			}
	 public void xspage(){
			int pageNum = getParaToInt(0, 1);
			int pageSize = getParaToInt("pageSize", PropKit.getInt("constants.pageSize"));
			String seller = getPara("seller");
			String sellerId = getPara("sellerId");
			String tel = getPara("tel");
			String beginDate = getPara("beginDate");
			String endDate = getPara("endDate");
			int date =getParaToInt("date", 0);
			renderResult(0,sellerTongService.findXSByPage(pageNum, pageSize, seller,sellerId, tel,beginDate,endDate,date));
		}
	 /**
	  * 编辑分公司
	  */
		public void toEdit(){
			String sellerId = getPara("sellerId");
			if(!StrKit.isBlank(sellerId)){
				setAttr("object", sellerTongService.findById(sellerId));
			}
			render("edit.jsp");
		}
		public void edit(){
			String sellerId=getPara("sellerId");
			int power=getParaToInt("power",0);
			if(!StrKit.isBlank(sellerId)&&power!=0){
				Seller seller=sellerTongService.findByStoreId(sellerId);
				seller.set("power", power);
			/*	if(power==2){
					//删除公司下的分公司
	    			List<Store> storeList=sellerTongService.findStoreBySeller(sellerId);
	    			if(storeList.size()>0){
	    				for (Store store : storeList) {
	    					store.set("state", 3).update();
	    				}
	    			}
				}*/
				if(sellerTongService.edit(seller)){
					renderSuccess(sellerTongService.findPageBySellerId(sellerId));
					return;
				}else{
					renderResult(1);
					return;
				}
			}else{
				renderResult(1);
				return;
			}
		}
		/**
		 * excel导出
		 */
		public void doExcel(){
			List<Pair> titles = new ArrayList<Pair>();
		    titles.add(new Pair("sellerId", "公司ID"));
		    titles.add(new Pair("sellerName", "公司名称"));
		    titles.add(new Pair("createtime", "入住时间"));
		    titles.add(new Pair("trueName", "公司联系人"));
		    titles.add(new Pair("contact", "联系电话"));
		    titles.add(new Pair("countStore", "分公司数量"));
		    titles.add(new Pair("countGoods", "产品数量"));
		    titles.add(new Pair("countSell", "产品订单量"));
		    titles.add(new Pair("countMoney", "销售额"));
		    titles.add(new Pair("power", "状态"));
			String seller = getPara("seller");
			String sellerId = getPara("sellerId");
			String tel = getPara("tel");
			String beginDate = getPara("beginDate");
			String endDate = getPara("endDate");
			int date =getParaToInt("date", 0);
			List<Record> list=sellerTongService.findExcelByPage(seller,sellerId, tel,beginDate,endDate,date);
			List<Record> listz=new ArrayList<Record>();
			for (int i = 0; i < list.size(); i++) {
				Record record=new Record();
					record.set("sellerId",list.get(i).get("sellerId"));
					record.set("sellerName", list.get(i).get("sellerName"));
					record.set("createtime", list.get(i).get("createtime"));
					record.set("trueName", list.get(i).get("trueName"));
					record.set("contact", list.get(i).get("contact"));
					record.set("countStore", list.get(i).get("countStore"));
					record.set("countGoods", list.get(i).get("countGoods"));
					record.set("countStore", list.get(i).get("countStore"));
					record.set("countSell", list.get(i).get("countSell"));
					record.set("countMoney", list.get(i).get("countMoney"));
					if(list.get(i).get("power").equals(1)){
						record.set("power", "开启");
					}else if(list.get(i).get("power").equals(2)){
						record.set("power", "关闭");
					}
				listz.add(record);
	 		}
			// 导出
			sellerTongService.export(getResponse(), getRequest(),titles, listz);		
			renderNull();
		}
}
